﻿using AutoMapper;
using Lingmeng.EntityFrameworkCore.Core.Base;
using System.Security.Cryptography.X509Certificates;
using Video.Application.Contract.Base;
using Video.Application.Contract.Videos;
using Video.Application.Contract.Videos.Dtos;
using Video.Application.Manage;
using Video.Domain.VideoRepository;
using Video.Domain.Videos;

namespace Video.Application.Videos
{
    public class VideoService : IVideoService
    {
        private readonly IVideoRepository _videoRepository;
        private readonly IMapper _mapper;
        private readonly CurrentManage _currentManage;
        private readonly IUnitOfWork _unitOfWork;
        public VideoService(IVideoRepository videoRepository,IMapper mapper,CurrentManage currentManage, IUnitOfWork unitOfWork)
        {
            _videoRepository = videoRepository;
            _mapper=mapper;
            _currentManage=currentManage;
            _unitOfWork=unitOfWork;
        }
        public async Task CreateAsync(CreateVideoInput createVideoInput)
        {
            var userId= _currentManage.GetId();
            var data=_mapper.Map<Domain.Videos.Video>(createVideoInput);
            data.UserId=userId;
            await _videoRepository.InserAsync(data);
            await _unitOfWork.SaveChangeAsync();
        }

        public async Task DeleteAsync(Guid id)
        {
            var video=await _videoRepository.FirstOfDefaultAsync(x=>x.UserId==_currentManage.GetId()&&x.Id==id);
            if(video!=null)
            {
               await _videoRepository.DeleteAsync(video);
               await _unitOfWork.SaveChangeAsync();
            }
            
        }
        public async Task AdminDeleteAsync(Guid id)
        {
            var video = await _videoRepository.FirstOfDefaultAsync(x => x.Id == id);
            if (video != null)
            {
                await _videoRepository.DeleteAsync(video);
                await _unitOfWork.SaveChangeAsync();
            }
        }

        public async Task<PageResultDto<GetVideoListDto>> GetListAsync(GetVideoInput videoInput)
        {
            var data =await _videoRepository.GetListVideoAsync(
                videoInput.UserId, videoInput.Classify, videoInput.Keywords, videoInput.StartTime, 
                videoInput.EndTime, videoInput.SkipCount, videoInput.MaxResultCount);
            var count =await _videoRepository.GetVideoCountAsync(videoInput.UserId, videoInput.Classify, videoInput.Keywords, videoInput.StartTime,
                videoInput.EndTime);
            var dto= _mapper.Map<List<GetVideoListDto>>(data);
            return new PageResultDto<GetVideoListDto>(count, dto);
        }

        public async Task CreateClassifyAsync(string name)
        {
            await _videoRepository.CreateClassifyAsync(name);
            
        }

        public async Task DeleteClassifyAsyn(Guid id)
        {
            await _videoRepository.DeleteClassifyAsyn(id);
        }

        public async Task UpdateClassifyAsync(ClassifyDto dto)
        {
           await _videoRepository.UpdateClassifyAsync(_mapper.Map<Classify>(dto));
           
        }

        public async Task<List<ClassifyDto>> GetClassifyListAsync(string? name)
        {
            var data = await _videoRepository.GetClassifyListAsync(name);
            var dto = _mapper.Map<List<ClassifyDto>>(data);
            return dto;
        }

        public async Task<bool> IsDeleteAsync(string path)
        {
            var data = await _videoRepository.FirstOfDefaultAsync(x => x.VideoAdress == path);
            if (data != null && data.UserId == _currentManage.GetId())
            {
                return true;
            }
            else
                return false;
        }
    }
}
